import { CssValue } from "./css";
import { Color, ColorOpts } from "./color";
import { Result } from "../result";

/**
 * 单阴影定义
 */
export declare type ShadowOpts = {
  // 水平阴影位置
  offsetX: number;
  // 垂直阴影位置
  offsetY: number;
  // 模糊距离,默认为0
  blur?: number;
  // 阴影尺寸(半径),默认为0
  spread?: number;
  // 颜色,默认为黑色
  color: ColorOpts;
  // 是否为内部阴影
  inset?: boolean;
};
export class Shadow extends CssValue<ShadowOpts> {
  protected parseCssValue(opts: ShadowOpts): Result<string> {
    const blur = opts.blur ? opts.blur : 0;
    const spread = opts.spread ? opts.spread : 0;
    const color = new Color(opts.color);
    let error: string | undefined;
    if ((error = color.cssValueError())) {
      return Result.fail(`解析阴影颜色失败:${error}`);
    }
    return Result.succ(
      `${opts.offsetX}px ${
        opts.offsetY
      }px ${blur}px ${spread}px ${color.cssValue()}${opts.inset ? "inset" : ""}`
    );
  }
}

/**
 * box阴影
 */
export declare type BoxShadowOpts = {
  shadows: ShadowOpts[];
};
export class BoxShadow extends CssValue<BoxShadowOpts> {
  protected parseCssValue(opts: BoxShadowOpts): Result<string> {
    if (!opts.shadows || opts.shadows.length == 0) {
      return Result.fail("至少需要一组阴影配置");
    }
    const shadows = [];
    let index: number, error: string | undefined;
    for (index = 0; index < opts.shadows.length; index++) {
      const s = new Shadow(opts.shadows[index]);
      if ((error = s.cssValueError())) {
        break;
      }
      shadows.push(s.cssValue());
    }
    if (error) {
      return Result.fail(`第${index}阴影配置错误:${error}`);
    }
    return Result.succ(shadows.join(","));
  }
}
